package com.example.watertest;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.ParallaxBackground;
import org.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.SimpleBaseGameActivity;

/**
 * Main class to create the instantiate the engine. This is also
 * single ton class that will create only one instance.
 * @author Yogesh Meshram
 *
 */
public class MainActivityBackPara extends SimpleBaseGameActivity {

	/**
	 * Set the camera height and width
	 */
	public static final int CAMERA_WIDTH = 800;
	public static final int CAMERA_HEIGHT = 480
			;
	private static final float SCREEN_MIN_CENTER_X = CAMERA_WIDTH * 0.5f - 25;
	private static final float SCREEN_MAX_CENTER_X = CAMERA_WIDTH * 0.5f + 25;
	private static final float SCROLL_TIME = 5;

	/**
	 * Constants for assets
	 */
	private final String CLOUD_GFX = "boton.png";
	private Camera mCamera;
	
	/**
	 * Texture Region for the game graphics
	 */
	private BitmapTextureAtlas mCloudBitmapTextureAtlas;
	private ITextureRegion mCloudTextureRegion;
	
	/**
	 * Game Variables
	 */
	private Scene mScene;
	private VertexBufferObjectManager vbo;
	private static MainActivityBackPara instance;
	

	@Override
	public EngineOptions onCreateEngineOptions() {
		
		instance = this;
		mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT){
			
			
			boolean incrementX = true;
			
			
			@Override
			public void onUpdate(float pSecondsElapsed) {

				
				final float currentCenterX = this.getCenterX();
				float offsetCenterX = 0;
				if(incrementX){
					offsetCenterX = currentCenterX + pSecondsElapsed * SCROLL_TIME;
					if(offsetCenterX >= SCREEN_MAX_CENTER_X){
						incrementX = false;
					}
				} else {
					
					offsetCenterX = currentCenterX - pSecondsElapsed * SCROLL_TIME;
					if(offsetCenterX <= SCREEN_MIN_CENTER_X){
						incrementX = true;
					}
				}
				this.setCenter(offsetCenterX, this.getCenterY());
				
				super.onUpdate(pSecondsElapsed);
			}
		};
		return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, 
				new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);

	}

	@Override
	protected void onCreateResources() {
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		this.mCloudBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 512, 512, 
											TextureOptions.BILINEAR);
		this.mCloudTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(mCloudBitmapTextureAtlas, this.getAssets(),CLOUD_GFX, 0, 0);
		
		this.mCloudBitmapTextureAtlas.load();
		
	}

	@Override
	protected Scene onCreateScene() {
		
		this.mScene = new Scene();
		this.vbo = this.getVertexBufferObjectManager();
		Sprite topCloud = new Sprite(0, CAMERA_HEIGHT /3, this.mCloudTextureRegion, this.vbo);
		
		Sprite midCloud = new Sprite(0, 0 , this.mCloudTextureRegion, this.vbo);
		
		Sprite bottomCloud = new Sprite(0, CAMERA_HEIGHT *.67f, this.mCloudTextureRegion, this.vbo);
		
		ParallaxBackground parallaxBackground = new ParallaxBackground(152/255f,212/255f,220/255f){

			float prevX = 0;
			float parallaxValueOffset = 0;
			
			
			@Override
			public void onUpdate(float pSecondsElapsed) {

				
				final float cameraCurrentX = mCamera.getCenterX();
				
				if (prevX != cameraCurrentX) {
					
					parallaxValueOffset +=  cameraCurrentX - prevX;
					this.setParallaxValue(parallaxValueOffset);
					prevX = cameraCurrentX;
				}

				super.onUpdate(pSecondsElapsed);
			}

		};
		
		parallaxBackground.attachParallaxEntity(new ParallaxEntity(5, topCloud));
		parallaxBackground.attachParallaxEntity(new ParallaxEntity(10, midCloud));
		parallaxBackground.attachParallaxEntity(new ParallaxEntity(15, bottomCloud));
		
		this.mScene.setBackground(parallaxBackground);
		this.mScene.setBackgroundEnabled(true);
		
		return this.mScene;
	}
	
	/**
	 * Simple Wrapper to get instance. 
	 * @return instance of MovingPlatformExampleActivity
	 */
	public static MainActivityBackPara getInstance(){
		return instance;
	}
		
}